# -*- coding:utf-8 -*-

from scrapy.selector import Selector
from scrapy.spiders import Spider
from scrapy.http import Request
from scrapy.http import FormRequest
import sys
reload(sys)
print sys.setdefaultencoding('utf-8')
class CsdnLogin(Spider):
    name = 'csdn'
    allowed_domain=['passport.csdn.net','csdn.net']
    headers = {
        'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
        'Accept-Encoding':'gzip,deflate,br',
        'Accept-Language':'zh-CN,zh;q=0.8',
        'Connection':'keep-alive',
        'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36'}
    def start_requests(self):
        print "touch page..."
        return [Request(url='https://passport.csdn.net/account/login',headers=self.headers,callback=self.parse_login)]

    def parse_login(self,response):
        print "starting login..."
        print response.url
        print response.meta['cookiejar']
        cookie = response.request.headers.getlist('Cookie')
        print cookie
        lt = response.xpath('//input[@name="lt"]/@value').extract_first()
        execution = response.xpath('//input[@name="execution"]/@value').extract_first()
        return [FormRequest.from_response(response,
            url='https://passport.csdn.net/account/login',
            formdata={
                'username':'301070179@qq.com',
                'password':'guang1302',
                '_eventId':'submit',
                'lt':lt,
                'execution':execution
            },
            headers=self.headers,
            # meta={'cookiejar':response.meta['cookiejar']},
            callback=self.after_login,
            )]

    def after_login(self,response):
        cookie = response.request.headers.getlist('Cookie')
        print cookie
        print 'successful....'
        print response.url
        return Request(url='http://my.csdn.net/my/mycsdn',callback=self.parse_content)
    def parse_content(self,response):
        print response.url
        "获取登录后的cookie值"
        cookie = response.request.headers.getlist('Cookie')
        print cookie
        print type(cookie)
        # Set-cookie = response.headers.getlist('Set-Cookie')
        # print Set-cookie
        # print type(Set-cookie)

        open("e:\hanhan\\csdn.txt","w+").write(response.body)